package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupCheckItemDao;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.SetmealCheckGroupDao;

import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.dto.QueryPageBeanDTO;
import com.itheima.health.pojo.entity.*;
import com.itheima.health.pojo.result.PageResult;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckGroupCheckItemDao checkGroupCheckItemDao;
    @Autowired
    private CheckItemDao checkItemDao;
    @Autowired
    private SetmealCheckGroupDao setmealCheckGroupDao;

    @Override
    public PageResult findPage(QueryPageBeanDTO queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupDao.selectByConditon(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void add(List<Integer> ids, CheckGroup checkGroup) {
        //验证checkGroup参数合法性
        String code = checkGroup.getCode();
        String name = checkGroup.getName();
        String sex = checkGroup.getSex();
        String helpCode = checkGroup.getHelpCode();
        String attention = checkGroup.getAttention();
        String remark = checkGroup.getRemark();
        if (code == null || code == "" || name == null || name == "" || sex == null || sex == "" ||
                helpCode == null || helpCode == "" || attention == null || attention == "" || remark == null || remark == ""){
            throw new BusinessRuntimeException("参数不合法");
        }

        //插入checkGroup表
        checkGroupDao.insert(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        //如果没有选择任何的checkitem到这就可以结束了
        if (ids.isEmpty()) return;

        //插入checkGroupCheckItem表
        List<CheckGroupCheckItem> collect = ids.stream().map(id -> new CheckGroupCheckItem(checkGroupId, id)).collect(Collectors.toList());
        checkGroupCheckItemDao.insertBatch(collect);
    }

    @Override
    public CheckGroup findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupCheckItemDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.selectById(id);
    }

    @Transactional
    @Override
    public void edit(List<Integer> ids, CheckGroup checkGroup) {
        //更新checkgroup基本信息
        checkGroupDao.update(checkGroup);

        //删除改checkgroup所有关联的item
        checkGroupCheckItemDao.delByCheckGroupId(checkGroup.getId());

        //插入新的
        List<CheckGroupCheckItem> collect = ids.stream().map(itemId -> new CheckGroupCheckItem(checkGroup.getId(), itemId)).collect(Collectors.toList());
        checkGroupCheckItemDao.insertBatch(collect);
    }

    @Transactional
    @Override
    public void delById(Integer id) {
        //检查是否被setmeal引用
        List<SetmealCheckGroup> setmealList = setmealCheckGroupDao.selectByCheckGroupId(id);
        if (!setmealList.isEmpty()) throw new BusinessRuntimeException("检查组被引用，无法删除");

        //删除CheckGroup
        checkGroupDao.delById(id);

        //删除CheckGroupCheckItem
        checkGroupCheckItemDao.delByCheckGroupId(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.selectAll();
    }
}
